package com.ruoyi.oos.self.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.oos.self.domain.TagConfigSelf;
import com.ruoyi.oos.self.domain.TagLimitdtlSelf;
import com.ruoyi.oos.self.mapper.TagConfigSelfMapper;
import com.ruoyi.oos.self.mapper.TagLimitdtlSelfMapper;
import com.ruoyi.oos.self.service.ITagConfigSelfService;
import com.ruoyi.oos.tag.domain.TagConfig;
import com.ruoyi.oos.tag.domain.TagLimitdtl;

/**
 * 测点配置Service业务层处理
 * 
 * @author wzy1
 * @date 2020-06-30
 */
@Service
public class TagConfigSelfServiceImpl implements ITagConfigSelfService 
{
    @Autowired
    private TagConfigSelfMapper tagConfigSelfMapper;
    
    @Autowired
    private TagLimitdtlSelfMapper limitSelfMapper;

    /**
     * 查询测点配置
     * 
     * @param id 测点配置ID
     * @return 测点配置
     */
    @Override
    public TagConfigSelf selectTagConfigSelfById(Long id)
    {
        return tagConfigSelfMapper.selectTagConfigSelfById(id);
    }

    /**
     * 查询测点配置列表
     * 
     * @param tagConfigSelf 测点配置
     * @return 测点配置
     */
    @Override
    public List<TagConfigSelf> selectTagConfigSelfList(TagConfigSelf tagConfigSelf)
    {
        return tagConfigSelfMapper.selectTagConfigSelfList(tagConfigSelf);
    }

    /**
     * 新增测点配置
     * 
     * @param tagConfigSelf 测点配置
     * @return 结果
     */
    @Override
    public int insertTagConfigSelf(TagConfigSelf tagConfig )
    {
         int a = tagConfigSelfMapper.insertTagConfigSelf(tagConfig);
        
        Long id = tagConfig.getId();
    	String upperval =tagConfig.getUpperval();//上线公式
    	String lowerval =tagConfig.getLowerval();//下线公式
    	String limitType =tagConfig.getLimittype();
        this.updateLimitdtl(upperval, id,1L,limitType);
        this.updateLimitdtl(lowerval, id,2L,limitType);
        return a;
    }

    /**
     * 修改测点配置
     * 
     * @param tagConfigSelf 测点配置
     * @return 结果
     */
    @Override
    public int updateTagConfigSelf(TagConfigSelf tagConfig)
    {

    	Long id = tagConfig.getId();
    	TagConfigSelf tagConfig1 = tagConfigSelfMapper.selectTagConfigSelfById(id);
    	String limitType =tagConfig.getLimittype();
    	String upperval =tagConfig.getUpperval();//上线公式
    	String lowerval =tagConfig.getLowerval();//下线公式
    	String upperval1 =tagConfig1.getUpperval();//上线公式
    	String lowerval1 =tagConfig1.getLowerval();//下线公式
    	if(StringUtil.isNotEmpty(upperval) && upperval.contains("{") && upperval.contains(":")  ) {
    		if(StringUtil.isNotEmpty(upperval1) && !upperval1.equals(upperval)) {
    			limitSelfMapper.deleteTagLimitdtlByConfigId(id,1);
    			this.updateLimitdtl(upperval, id, 1L,limitType);
    		}
    	}
    	if(StringUtil.isNotEmpty(lowerval) && lowerval.contains("{") && lowerval.contains(":")  ) {
    		if(StringUtil.isNotEmpty(lowerval1) && !lowerval1.equals(lowerval)) {
    			limitSelfMapper.deleteTagLimitdtlByConfigId(id,2);
    			this.updateLimitdtl(lowerval, id, 2L,limitType);
    		}
    	}
        return tagConfigSelfMapper.updateTagConfigSelf(tagConfig);
    
    	
       // return tagConfigSelfMapper.updateTagConfigSelf(tagConfigSelf);
    }

    /**
     * 删除测点配置对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTagConfigSelfByIds(String ids)
    {
        return tagConfigSelfMapper.deleteTagConfigSelfByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除测点配置信息
     * 
     * @param id 测点配置ID
     * @return 结果
     */
    public int deleteTagConfigSelfById(Long id)
    {
        return tagConfigSelfMapper.deleteTagConfigSelfById(id);
    }
    
    /**
     * 更新公式
     * @param upperval
     * @param id
     * @param type
     */
    private void  updateLimitdtl(String upperval,Long id,Long type,String limitType) {
    	
    	if(limitType.equals("1") || limitType.equals("2") || limitType.equals("4") ) {
        if(StringUtils.isNotEmpty(upperval) && upperval.contains("{") && upperval.contains(":")) {
       	 upperval = upperval.replace("{", "").replace("}", "");
       	String[]  name1 = upperval.split("\\|");
       	for (String string : name1) {
   			String[] name2 = string.split(":");
   			String[] times = name2[0].split(",");
   			String[] limitvalue = name2[1].split(",");
   			int size = limitvalue.length;
   			for (int i =0; i<size;i++) {
   				TagLimitdtlSelf limitdtl = new TagLimitdtlSelf();
   				limitdtl.setConfigid(id);
   				limitdtl.setType(type);
   				limitdtl.setStarttime(times[0]);
   				limitdtl.setEndtime(times[1]);
   				limitdtl.setLevel(i+"");
   				limitdtl.setLimitval(Float.parseFloat(limitvalue[i]));
   				if(i+1<size) {
   					limitdtl.setEndlimitval(Float.parseFloat(limitvalue[i+1]));
   				}else if(i+1==size) {
   					limitdtl.setEndlimitval(Float.parseFloat(limitvalue[i]));
   				}
   				
   				limitSelfMapper.insertTagLimitdtlSelf(limitdtl);
   			}
   		}
        }
    	}
    	//{[2020-01-01,2050-01-01]:<(100,30)(150,100)&>}|
    	if(limitType.equals("3")) {//动态限值
    		 if(StringUtils.isNotEmpty(upperval) && upperval.contains("{") && upperval.contains(":") ) {	
    			 upperval = upperval.replace("{", "").replace("}", "");
    				String[]  name1 = upperval.split("\\|"); 
    				for (String string : name1) {
    		   			String[] name2 = string.split(":");
    		   			String[] times = name2[0].split(",");
    		   			String tempTag =name2[1];
    		   			String[] limitvalue = name2[2].split("&");
    		   			int size = limitvalue.length;
    		   			for (int i =0; i<size;i++) {
    		   				TagLimitdtlSelf limitdtl = new TagLimitdtlSelf();
    		   				limitdtl.setConfigid(id);
    		   				limitdtl.setType(type);
    		   				limitdtl.setStarttime(times[0]);
    		   				limitdtl.setEndtime(times[1]);
    		   				limitdtl.setLevel(i+"");
//    		   				limitdtl.setLimitval(Float.parseFloat(limitvalue[i]));
    		   				String limitval ="curve('"+tempTag+"',"+limitvalue[i]+")";
    		   				limitdtl.setPitag(limitval);
//    		   				if(i+1<size) {
//    		   					limitdtl.setEndlimitval(Float.parseFloat(limitvalue[i+1]));
//    		   				}else if(i+1==size) {
//    		   					limitdtl.setEndlimitval(Float.parseFloat(limitvalue[i]));
//    		   				}
    		   				
    		   				limitSelfMapper.insertTagLimitdtlSelf(limitdtl);
    		   			}
    		   		}	
    		 }
    		
    	}
    }

    
}
